<?php
class MantPrev extends AppModel {

	var $name = 'MantPrev';
	var $useTable = 'mant_prevs';
	var $displayField = 'descripcion';		

	var $validate = array(
		'id' => VALID_NOT_EMPTY,
		'descripcion' => VALID_NOT_EMPTY,
		'frecuencia_km' => VALID_NOT_EMPTY,
	);

	var $hasAndBelongsToMany = array(
		'Vehiculo' => array('className'    => 'Vehiculo',
                    'joinTable'    => 'mant_prevs_vehiculos',
                    'foreignKey'   => 'mant_prev_id',
                    'associationForeignKey'=> 'vehiculo_id',
                    'conditions'   => '',
                    'order'        => '',
					'limit'        => '',
                    'unique'       => true,
                    'finderQuery'  => '',
                    'deleteQuery'  => '',) ,
    );

    function obtenerMantenimientoPreventivo(){    	
    	
    	$resultado = $this->query(
    		"SELECT MAX(mant_prevs_vehiculos.fecha_fin) AS fecha, mant_prevs.descripcion, mant_prevs.frecuencia_km, vehiculos.placas
    		FROM mant_prevs, mant_prevs_vehiculos, vehiculos 
    		WHERE mant_prevs_vehiculos.mant_prev_id = mant_prevs.id AND mant_prevs_vehiculos.vehiculo_id = vehiculos.id 
    			  AND (vehiculos.km - mant_prevs_vehiculos.km) >= mant_prevs.frecuencia_km GROUP BY vehiculos.id, mant_prevs_vehiculos.mant_prev_id");

    	return $resultado;
    }
}
?>
